<?php
/**
 * User: lie
 * Date: 2020-11-07 23:29
 * description: 员工信息管理
 */
    include('conn.php');
    $sql = "SELECT * FROM customer";            // SQL Sentence
    $result = $mysqli->query($sql);             // Perform a query on the
    echo "<table border='1'>";
    for ($row = $result->num_rows - 1; $row >= 0; $row--){  // 获取结果集中的行数
        if (!$result->data_seek($row)){
            echo "Cannot seek to row $row".$mysqli->error."\n";
            continue;
        }
        if (!($obj = $result->fetch_object())){
            continue;
        }
        echo "
            <tr>
                <td>$obj->cCusCode</td>
                <td>$obj->cCusName</td>
                <td>$obj->cCusAddName</td> 
                <td>
                    <a href='?id=$obj->cCusCode&delete=1'><b>删除</b></a>
                    <a href='?id=$obj->cCusCode&update=1'><b>修改</b></a>
                </td>                           
            </tr>";
    }

    $result->close();   // 释放结果集

    if ($_GET['delete'] == 1){
        $del = "DELETE FROM customer WHERE cCusCode='$_GET[id]'";
        var_dump($del);
        var_dump($mysqli->query($del));
        if ($mysqli->query($del)){
            echo "<script>alert('删除成功')</script>";
        }
    }
    if ($_GET['update'] == 1){
        $sql = "SELECT * FROM customer WHERE cCusCode = $_GET[id]";
        $result = $mysqli->query($sql);
        $obj = $result->fetch_object();

        echo "</table>";
    ?>

    <form action="" method="post">
        <input type="text" name="cCusCode" id="" value="<?php echo $obj->cCusCode; ?>" disabled>
        <input type="text" name="cCusName" id="" value="<?php echo $obj->cCusName; ?>">
        <input type="text" name="cCusAddName" id="" value="<?php echo $obj->cCusAddName; ?>">
        <input type="submit" name="sub" id="" value="提交">
    </form>

<?php
    if ($_POST['sub']){
        $sql = "UPDATE customer SET cCusName = '$_POST[cCusName]', cCusAddName = '$_POST[cCusAddName]'  WHERE  cCusCode = '$_GET[id]'";
        if ($mysqli->query($sql)){
            echo "<script>alert('修改成功');window.location.href='index.php';</script>";
        }
    }
    }
    $mysqli->close();   // 关闭连接


    // // 面向对象,返回对象
    // while ($obj = $result->fetch_object()){
    //     var_dump($obj);
    //     var_dump($obj->cCusCode);
    // }
?>
